/**
 * 国际化配置
 * 
 * @author CT
 * @email 994599447@qq.com
 * @date 2017.8.14
 */

import * as DeviceInfo from 'react-native-device-info';
import I18n from 'react-native-i18n';

//语言包
import zh_CN from './language/zh-CN';
import zh_TW from './language/zh-TW';

//当前设备语言
export const deviceLocale = DeviceInfo.getDeviceLocale() ? DeviceInfo.getDeviceLocale() : 'en_US';


/**
 * 初始化语言配置信息
 */
export const initLocale = () => {
    //添加语言包
    I18n.translations = {
        // en_US,
        zh_CN,
        zh_TW
    };
    I18n.locale = handleLocale();
    
}

/**
 * 获取字符串
 * @param {string} key 
 * @param {Object} obj 
 */
export const t = (key,obj) => {
    let rst = I18n.t(key,obj);
    return rst;
}

/**
 * 处理设备语言环境，默认zh_CN
 * return 语言，示例：zh_CN
 */
const handleLocale = () => {
    switch(deviceLocale.split('-')[0]){
        case 'zh':
            return getZh();
        default:
            return 'zh_CN';
    }
}


/**
 * 获取中文语言包
 */
const getZh = () => {
    switch(deviceLocale){
        case 'zh-TW':
            return 'zh_TW';
        case 'zh-Hant-TW':
            return 'zh_TW';
        default:    //默认中文简体
            return 'zh_CN';
    }
}
